import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/base.css'
import './assets/iconfont/iconfont.css'
import './assets/iconfont/iconfont.js'
import { getLocalStorage, setLocalStorage, removeLocalStorage } from './utils/localStorage'

Vue.config.productionTip = false
Vue.prototype.$getLocalStorage = getLocalStorage
Vue.prototype.$setLocalStorage = setLocalStorage
Vue.prototype.$removeLocalStorage = removeLocalStorage

Vue.use(ElementUI)

// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.path === '/login') {
    return next()
  } else if (to.name === null) {
    return next('/login')
  } else {
    const token = getLocalStorage('sps_token')
    if (token) {
      next()
    } else {
      return next('/login')
    }
  }
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
